import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue"
import { useEnumStore } from "@/store/enumeration"
import { EquityPortfolioPagePost, EquityPortfolioDeleteDel, EquityPortfolioDetailGet } from "@/api/equity"
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';

export const protfolioListStore = defineStore("protfolioListStoreId", () => {
    const { global } = useInstance();
    const portfolioRef = ref<{ show: (type: string, row?: any) => void }>()
    const loopRef = ref<{ show: (type: string, row?: any) => void }>()


    const { packageUseType ,grantType,expiryPeriodType,schemeConfigletailsType} = useEnumStore()
    const detailData = ref([
        {
            prop: "supplierCode",
            name: "供应商编码",
            type: "text",
            value: null
        }
    ])
    const tableProp = reactive(
        {
            name: "packageList",
            propsList: [
                {
                    prop: 'portfolioName',
                    label: '组合名称',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'portfolioCode',
                    label: '组合编码',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                // {
                //     prop: 'packageCode',
                //     label: '包编码',
                //     type: 'normal',
                //     minWidth: '150px',
                //     hidden: true,
                // },
                {
                    prop: 'equityType',
                    label: '权益分类',
                    type: 'normal',
                    minWidth: '120px',
                    hidden: true,
                },
                {
                    prop: 'portfolioUsageMode',
                    label: '使用方式',
                    type: 'normal',
                    minWidth: '120px',
                    hidden: true,
                },
                {
                    prop: 'grantType',
                    label: '发放方式',
                    type: 'normal',
                    minWidth: '120px',
                    hidden: true,
                },
                {
                    prop: 'receiveUrl',
                    label: '领取链接',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                // {
                //     prop: 'minusMoney',
                //     label: '立减金额',
                //     type: 'normal',
                //     minWidth: '90px',
                //     hidden: true,
                // },
                {
                    prop: 'grantIntervalTime',
                    label: '发放间隔时间',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'monthGetNum',
                    label: '月领取数量',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                // {
                //     prop: 'grantNum',
                //     label: '发放数量',
                //     type: 'normal',
                //     minWidth: '90px',
                //     hidden: true,
                // },
        
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        condition: {
            packageCode:null,//包编码
            portfolioCode:null,//组合编码
            portfolioName:null,//组合名称
            equityType:null,//权益类型
        },
        orders: ["created_time desc"],
        pageNum: 20,
        pageSize: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []
    })

    const getDate1 = async (type: string) => {
        pageParams.loadIng = true

        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                // @ts-ignore
                listSearch.condition[it] = null
            })
        }
        try {
            const res = await EquityPortfolioPagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        firstGrantIntervalTime:item.firstGrantIntervalTime +' ' +  expiryPeriodType[item.firstTimeUnit -1]?.value,
                        grantIntervalTime:item.grantIntervalTime +' ' +  expiryPeriodType[item.timeUnit -1]?.value,
                        grantType: grantType[item.grantType -1]?.value,
                        portfolioUsageMode: packageUseType[item.portfolioUsageMode -1]?.value,
                        equityType:schemeConfigletailsType[item.equityType -1]?.value,
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false
        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await EquityPortfolioDetailGet({ id: row.id })
            loopRef.value?.show('edit', res.data)
        } catch (error) {
            console.log(error);
        }
    }
    const quotaBtn = async (row: any) => {
        // quotaRef.value?.show('add', row)

    }
    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.portfolioName}`)
        if (flag) {
            const res = await EquityPortfolioDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const AddEdit = async (type: string, row?: any) => {
        if (type == 'add') {
            portfolioRef.value?.show(type)
        } else {
            const res = await EquityPortfolioDetailGet({ id: row.id })
            portfolioRef.value?.show(type, res.data)
        }
    }
    return {
        detailData, tableProp, listSearch, pageParams, portfolioRef, loopRef,
        getDate1, handPage, delBtn, detailBtn, quotaBtn, AddEdit
    }
})